package com.exam.controller.student;




import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.exam.common.vo.ExamStuVO;
import com.exam.common.vo.ExamTeacherVO;
import com.exam.common.vo.MenuVO;
import com.exam.common.vo.UserVO;
import com.exam.common.vo.page.PageModel;
import com.exam.entity.ExamStu;
import com.exam.service.role.RoleService;
import com.exam.service.student.StuService;
import com.exam.util.SHAUtil;

@Controller
@RequestMapping(value = "/stu")
public class StudentController {

	@Autowired
	RoleService roleService;
	
	@Autowired
	StuService stuService;

	@RequestMapping(value = "/stuList")
	public String list(Model model,HttpSession session) throws Exception {
		List<MenuVO> mnList = new ArrayList<MenuVO>();
		ExamTeacherVO teaVO = (ExamTeacherVO) session.getAttribute("teacher");
		if(teaVO!=null){
		UserVO userVO = new UserVO();
		userVO.setUserName(String.valueOf(teaVO.getExamTeacherNumber()));
		userVO.setUserPwd(teaVO.getExamTeacherPassword());
		
		mnList = roleService.getMenuVO(userVO);
		session.setAttribute("mnList",mnList);
		}
		PageModel<ExamStuVO> page = stuService.findByPage();
		/*SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		for(int i = 0;i<page.getDatas().size();i++){
			df.format(page.getDatas().get(i).getExamStuBrithday());
		}*/
		
		model.addAttribute("page", page);
		model.addAttribute("dplay", "display:none;");
		return "/student/list";
	}

	@RequestMapping(value = "/delete/{stuId}")
	public String delete(@PathVariable String stuId, RedirectAttributes redirectAttributes) {
		ExamStu stu = new ExamStu();
		stu.setExamStuId(Long.parseLong(stuId));
		stuService.delete(stu);
		redirectAttributes.addFlashAttribute("message", "删除成功");
		return "redirect:/stu/stuList";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		ExamStuVO stuVO = new ExamStuVO();
		model.addAttribute("stuVO", stuVO);
		return "/student/add";
	}

	@RequestMapping(value = "/addStu", method = RequestMethod.POST)
	public String add(@Validated ExamStuVO stuVO, BindingResult br, Model model,
			RedirectAttributes redirectAttributes) {
		//ExamStuVO sVO = stuService.findByStuNumber(stuVO.getExamStuNumber());
		if (br.hasErrors()) {
			model.addAttribute("stuVO", stuVO);
			return "/student/add";
		}/*else{
			if (sVO != null) {
				model.addAttribute("stuVO", stuVO);
				model.addAttribute("message", "账号已存在");
				return "/student/add";
			}
		}*/
		if (stuVO != null) {
			ExamStu stu = new ExamStu();
			if (stuVO.getExamStuDone() == null) {
				stuVO.setExamStuDone(0);
			}
			BeanUtils.copyProperties(stuVO, stu);
			try {
				stu.setExamStuPassword(SHAUtil.shaEncode(stuVO.getExamStuPassword()));
				stuService.save(stu);
			} catch (Exception e) {
				//e.printStackTrace();
				model.addAttribute("stuVO", stuVO); 
				model.addAttribute("message", "账号已存在");
				return "/student/add";
			}
				
				
			redirectAttributes.addFlashAttribute("message", "添加成功");
		}
		return "redirect:/stu/stuList";
	}

	@RequestMapping(value = "/edit/{examStuId}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable String examStuId) {
		ExamStu stu = stuService.findByKey(Long.parseLong(examStuId));
		ExamStuVO stuVO = new ExamStuVO();
		BeanUtils.copyProperties(stu, stuVO);
		stuVO.setExamStuPassword("");
		model.addAttribute("stuVO", stuVO);
		return "/student/edit";
	}

	@RequestMapping(value = "/edit/{examStuId}", method = RequestMethod.POST)
	public String edit(@PathVariable String examStuId, @Validated ExamStuVO stuVO, BindingResult br,
			RedirectAttributes redirectAttributes) {
		if (br.hasErrors()) {
			return "redirect:/stu/edit/{examStuId}";
		}
		ExamStu stu = new ExamStu();
		BeanUtils.copyProperties(stuVO, stu);
		try {
			stu.setExamStuPassword(SHAUtil.shaEncode(stuVO.getExamStuPassword()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		stu.setExamStuId(Long.parseLong(examStuId));
		stuService.update(stu);
		redirectAttributes.addFlashAttribute("message", "修改成功");
		return "redirect:/stu/stuList";
	}

	
	@RequestMapping(value="/search", method = RequestMethod.POST)
	public String search(String searchTip,Model model,String tipCheck){
		//System.out.println(tipCheck);
		String check1 = "";  String check2="";  String check3="";
		if(tipCheck == null){
			tipCheck = "";
		}
		String[] n = tipCheck.trim().split(",");
		PageModel<ExamStuVO>  page = stuService.findBySearchType(n, searchTip);
		model.addAttribute("page",page);
		if(searchTip!=null | !searchTip.equals("")){
			model.addAttribute("searchTip",searchTip);
		}
		for(int i =0;i<n.length;i++){
			if(n[i]=="1"||n[i].equals("1")){
				check1="checked";
			}else if(n[i]=="2"||n[i].equals("2")){
				check2="checked";
			}else if(n[i]=="3"||n[i].equals("3")){
				check3="checked";
			}
		}
		model.addAttribute("check1",check1);
		model.addAttribute("check2",check2);
		model.addAttribute("check3",check3);
		model.addAttribute("search","search");
		//model.addAttribute("dplay", "display:none;");
		return "/student/list";
	}
}
